#!/bin/sh

DIR_SHADOW=${DIR_SHADOW:=/tmp/hxLinux}
if [ ! -d "$DIR_SHADOW" ]; then
	mkdir -p "$DIR_SHADOW" && chmod 777 "$DIR_SHADOW"
fi

HXSYSTEM=${HXSYSTEM:=/hxData/hxSystem}
HXLINUX=${HXLINUX:=$HXSYSTEM/trunk/Linux}
HXUTL=${HXUTL:=$HXLINUX/script/utility}


confirm(){ #{{{
	printf '%s%s' $1 '(Y/n):'
	local Answer=
	read Answer
	[ 'Y' = $Answer ]
}
#}}}

sourceShellFile(){ #{{{
	local F_SH_TAR="$HXUTL/hxShell.tar.xz"
	local F_SH="$DIR_SHADOW/hxShell/$1.sh"
	[ ! -e "$DIR_SHADOW/hxShell" ] &&
		mkdir -p "$DIR_SHADOW" &&
		tar -C "$DIR_SHADOW" -xJf "$F_SH_TAR"
	. "$F_SH"

	return
	[ ! "$F_SH_TAR" -ot "$F_SH" ] &&
		mkdir -p "`dirname \"$F_SH\"`" &&
		xz -dc "$F_SH_TAR" | tar -Oxf - "hxShell/$1.sh" >"$F_SH"

	. "$F_SH"
}
#}}}
extractPythonFile(){ #{{{
	local F_PY_TAR="$HXUTL/hxPython.tar.xz"
	local F_PY="$DIR_SHADOW/hxPython/$1.py"
	[ ! -e "$DIR_SHADOW/hxPython" ] &&
		mkdir -p "$DIR_SHADOW" &&
		tar -C "$DIR_SHADOW" -xJf "$F_PY_TAR"
	[ -f "$F_PY" ] &&
		echo "$F_PY"

	return
	[ ! "$F_PY" -nt "$F_PY_TAR" ] &&
		mkdir -p "`dirname \"$F_PY\"`" &&
		xz -dc "$F_PY_TAR" | tar -Oxf - "hxPython/$1.py" >"$F_PY"

	[ -f "$F_PY" ] &&
		echo "$F_PY"
}
#}}}


#canShutdown(){ #{{{
#	#ps aux | grep -E '[r]torrent' >/dev/null
#	if pgrep 'rtorrent|tmux'; then
#	#if [ 0 -eq $? ]; then
#		tput bold;tput setf 5;tput rev;
#		echo 'rtorrent or tmux is running!'
#		tput sgr0 
#		return 1
#	elif [ -d /tmp/rtorrent ]; then
#		tput bold;tput setf 5;tput rev;
#		echo '/tmp/rtorrent'
#		tput sgr0 
#		return 2
#	fi
#	return 0
#}
##}}}
rmDNS(){ #{{{
	pdnsd-ctl record $1 invalidate &&
		pdnsd-ctl record $1 delete
}
#}}}
encrypt(){ #{{{
	local F_IN="$1"
	local F_OUT="$2"
	local EXT=${F_OUT##*.}
	local CMD='openssl enc -e'

	[ -z $EXT ] && exit 7
	[ -e "$F_OUT" ] && (confirm "Do you want to overwrite $F_OUT?" || exit)
	$CMD -$EXT -in "$F_IN" -out "$F_OUT"
}
#}}}
decrypt(){ #{{{
	local F_IN="$1"
	local F_OUT="$2"
	local EXT=${F_IN##*.}
	local CMD='openssl enc -d'
	
	[ ! -n "$EXT" ] && return 7
	[ $F_OUT ] || return 9
	[ -e "$F_OUT" ] && (confirm "Do you want to overwrite $F_OUT?" || exit)
	$CMD -$EXT -in "$F_IN" -out "$F_OUT"
}
#}}}
fltimg(){ #{{{
	local F_IN="$1"
	local PRE_URL="$2"
	local REGEX='(?<=href=").+?\.(jpg|JPG)(?=")' 

	if [ ! -z $PRE_URL ]; then
		grep -oP "$REGEX" "$F_IN" |
			uniq |
			sed "s=^=$PRE_URL=" |
			tee f.lst | nl
	else
		exit $#
	fi
}
#}}}
getArguments(){ #{{{
	local ARGS="`seq $1 $2`"
	[ "$ARGS" ] || return 6
	ARGS=`printf '"$%d" ' $ARGS`
	echo -n $ARGS
}
#}}}


[ 0 -eq $# ] && exit 98
FUNC="$1"
if expr "$0" : .*svnDiff.sh >/dev/null 2>&1; then
	FUNC='svnDiff'
elif expr "$0" : .*svnMerge.sh >/dev/null 2>&1; then 
	FUNC='svnMerge'
fi

case "$FUNC" in #{{{
	hxpy)
		F_PY="$2"
		[ "$F_PY" ] || exit
		F_PY=`extractPythonFile "$F_PY"` || exit
		[ -n "$F_PY" ] &&
			eval python "$F_PY" `getArguments 3 $#`
		exit;;
	canShutdown|rmDNS|encrypt|decrypt|fltimg)
		;;
	svnMerge|svnDiff)
		sourceShellFile hxCvs|| exit
		eval "$FUNC" `getArguments 1 $#`
		exit;;
	*)
		sourceShellFile "$FUNC" || exit;;
esac #}}}

eval "$FUNC" `getArguments 2 $#`
